package com.zcxy.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zcxy.constant.CommonConstant;
import com.zcxy.core.bean.PageQuery;
import com.zcxy.mybatis.service.BaseServiceImpl;
import com.zcxy.sys.entity.SysRoleUser;
import com.zcxy.sys.mapper.SysRoleUserMapper;
import com.zcxy.sys.pojo.SysRoleUserVo;
import com.zcxy.sys.service.ISysRoleUserService;
import com.zcxy.util.BeanCopierUtil;
import com.zcxy.util.FileUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 角色_用户Service业务层处理
 *
 * @author zcc
 * @email 860474958@qq.com
 * @date 2020-06-04
 */
@Service
@Slf4j
public class SysRoleUserServiceImpl extends BaseServiceImpl<SysRoleUserMapper, SysRoleUser>
        implements ISysRoleUserService {
    @Override
    @Transactional(rollbackFor = Exception.class)
    public IPage<SysRoleUserVo> queryPageList(PageQuery<SysRoleUser> queryVo) {
        QueryWrapper queryWrapper = new QueryWrapper(queryVo.getT() == null ? new SysRoleUser() : queryVo.getT());
        Page page = new Page(queryVo.getPageNo(), queryVo.getPageSize());
        IPage<SysRoleUser> sysUserVoIPage = baseMapper.selectPage(page, queryWrapper);
        return BeanCopierUtil.copyPage(sysUserVoIPage, SysRoleUserVo.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<SysRoleUserVo> queryList(SysRoleUser queryVo) {

        return null;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addSysRoleUser(SysRoleUser queryVo) {
        queryVo.setDelFlag(CommonConstant.DEL_FLAG_1);
        //TODO 具体新增业务限制各自实现
        baseMapper.insert(queryVo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateSysRoleUser(SysRoleUser queryVo) {
        //查询数据库的实体(主键校验在controller的校验器里面)
        SysRoleUser sysRoleUserDb = baseMapper.selectById(queryVo.getId());
        BeanCopierUtil.copyObject(queryVo, sysRoleUserDb);
        baseMapper.updateById(sysRoleUserDb);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeSysRoleUser(String id) {
        baseMapper.deleteById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeAllSysRoleUser(List<String> ids) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.in("id", ids);
        baseMapper.delete(queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void download(SysRoleUser queryVo, HttpServletResponse response) throws IOException {
        List<SysRoleUser> sysRoleUsers = baseMapper.selectList(new QueryWrapper<>(queryVo));
        List<Map<String, Object>> list = new ArrayList<>();
        for (SysRoleUser sysRoleUserExcel : sysRoleUsers) {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("主键", sysRoleUserExcel.getId());
            map.put("用户表主键", sysRoleUserExcel.getUserId());
            map.put("角色表主键", sysRoleUserExcel.getRoleId());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

}
